package com.yy.device.boot.handler;

import com.alibaba.fastjson.JSON;
import com.yy.device.boot.DeviceDataContainer;
import com.yy.device.boot.protocol.IMessage;
import com.yy.device.boot.protocol.RegisterMessage;
import com.yy.device.boot.protocol.ReportMessage;
import com.yy.device.boot.protocol.TaskMessage;
import com.yy.device.boot.protocol.TaskQueryMessage;
import com.yy.device.core.entity.Task;
import com.yy.device.core.service.TaskService;
import com.yy.device.enums.DeviceExeTaskStatus;
import com.yy.util.ApplicationContextUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.Objects;


/**
 * 设备服务器处理程序
 *
 * @author sunzhiqiang
 * @date 2021/12/14 14:37:16
 */
@Slf4j
public class ServerHandler extends SimpleChannelInboundHandler<IMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, IMessage message) throws Exception {
        if (message instanceof RegisterMessage) {
            RegisterMessage registerMessage = (RegisterMessage) message;
            DeviceDataContainer.register(registerMessage.getDeviceCode(), ctx);
            log.info("a device register success,deviceCode:{}", registerMessage.getDeviceCode());
        } else if (message instanceof TaskMessage) {
            log.info("TaskMessage:{}", JSON.toJSON(message));
            TaskMessage taskMessage = (TaskMessage) message;
            TaskService taskService = ApplicationContextUtil.getBean(TaskService.class);
            if (Objects.isNull(taskService)) {
                return;
            }
            Task newTask = taskService.findNewByTaskId(taskMessage.getTaskId());
            if (Objects.isNull(newTask)) {
                log.info("report message :{}", JSON.toJSONString(taskMessage));
                return;
            }
            newTask.setExeStatus(DeviceExeTaskStatus.COMPLETED.name());
            newTask.setResult(JSON.toJSONString(taskMessage));
            newTask.setUpdateTime(new Date());
            taskService.updateById(newTask);
        } else if (message instanceof TaskQueryMessage) {
            log.info("TaskMessage:{}", JSON.toJSON(message));
            TaskQueryMessage taskMessage = (TaskQueryMessage) message;
            TaskService taskService = ApplicationContextUtil.getBean(TaskService.class);
            if (Objects.isNull(taskService)) {
                return;
            }
            Task newTask = taskService.findNewByTaskId(taskMessage.getTaskId());
            if (Objects.isNull(newTask)) {
                log.info("report message :{}", JSON.toJSONString(taskMessage));
                return;
            }
            newTask.setExeStatus(DeviceExeTaskStatus.COMPLETED.name());
            newTask.setResult(JSON.toJSONString(taskMessage));
            taskService.updateById(newTask);
        } else if (message instanceof ReportMessage) {
            ReportMessage reportMessage = (ReportMessage) message;
            Integer deviceCode = DeviceDataContainer.getDeviceCode(ctx);
            if (Objects.isNull(deviceCode)) {
                return;
            }
            DeviceDataContainer.DEVICE_DATA_MAP.put(deviceCode, reportMessage.getData());
        }

    }

}
